package org.example.framework;

import org.example.framework.protocol.http.HttpClient;
import org.example.framework.register.RemoteMapRegister;
import org.example.provider.api.HelloService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.util.*;

public class ProxyFactory {

    public static <T> T getProxy(Class<?> interfaceClass, String version, List<String> tags) {

        // 获取用户配置 jdk cglib
        Object proxyInstance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return toSend(new Invocation(interfaceClass.getName(), method.getName(), method.getParameterTypes(), args, version, tags));
            }
        });
        return (T) proxyInstance;
    }


    public static <T> T getProxy(Class<?> interfaceClass, String version) {

        // 获取用户配置 jdk cglib
        Object proxyInstance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return toSend(new Invocation(interfaceClass.getName(), method.getName(), method.getParameterTypes(), args, version));
            }
        });
        return (T) proxyInstance;
    }

    public static <T> T getProxy(Class<?> interfaceClass) {

        // 获取用户配置 jdk cglib
        Object proxyInstance = Proxy.newProxyInstance(ProxyFactory.class.getClassLoader(), new Class[]{interfaceClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return toSend(new Invocation(interfaceClass.getName(), method.getName(), method.getParameterTypes(), args));
            }
        });
        return (T) proxyInstance;
    }


    private static String toSend(Invocation interfaceClass) {
        Invocation invocation = interfaceClass;
        HttpClient httpclient = new HttpClient();
        Map<String, List<URL>> listMap = RemoteMapRegister.get(interfaceClass.getInterfaceName());
        if (listMap == null) {
            listMap = new HashMap<>();
        }
        List<String> tags = interfaceClass.getTags();
        Set<URL> urlSet = new HashSet<>();
        if (listMap.containsKey("")) {
            // all
            for (List<URL> value : listMap.values()) {
                urlSet.addAll(value);
            }
        } else {
            for (String tag : tags) {
                List<URL> urlList = listMap.get(tag);
                if (urlList == null) {
                    urlList = new ArrayList<>();
                }
                urlSet.addAll(urlList);
            }
        }
        if (urlSet.isEmpty()) {
            throw new IllegalStateException("注册中心未找到符合tag条件的配置信息");
        }
        URL url = LoadBalance.random(urlSet);
        return httpclient.send(url.getHost(), url.getPort(), invocation);
    }
}
